CC=gcc
CCFLAGS=-g -Wall -Werror -ggdb3
LDFLAGS=
OBJS=main.o
APPNAME=app

##################################
#    Method 1 - using objcopy	 #
##################################
#OBJCOPY_WAY=You should uncomment this line to use it, bro

ARCH=$(shell arch)
ELF_TYPE=elf64-x86-64
BIN_ARCH=i386:x86-64
ifeq ($(ARCH),i386)
	override ELF_TYPE=elf32-x86-64
	override BIN_ARCH=i386
endif
OBJCOPY=objcopy --input=binary --output=$(ELF_TYPE) --binary-architecture $(BIN_ARCH)

ifdef OBJCOPY_WAY
	OBJS += textdata.o
else

##################################
# Method 2 - using linker script #
##################################
	LDSCRIPT=script.ld
endif

all : $(OBJS)
	$(CC) $(LDFLAGS) $(LDSCRIPT) $(OBJS) -o $(APPNAME)

.PHONY : all

test : all
	./$(APPNAME)

.PHONY : test

clean :
	rm -rf *.o $(APPNAME)

.PHONY : clean

textdata.o : textdata.txt
	$(OBJCOPY) $< $@

%.o : %.c
	$(CC) $(CCFLAGS) -c $< -o $@
